package preprocess;

import java.io.*;

/**
 * XML接收类
 * 
 * @author yan
 * @version 0.1
 */
public class XmlReceive extends Thread
{
    private PipedInputStream Receiver;

    public XmlReceive(int getSize)
    {
        Receiver = new PipedInputStream(getSize);
    }

    public PipedInputStream getInputStream()
    {
        return Receiver;
    }

    public void run()
    {
        try
        {
            byte[] byteStr = null;
            int in = 0;
            int tmp = 0;
            while (!XmlTransfer.isEnd)
            {
                XmlTransfer.critical.getReadLock();
                
                // start to receive a new document
                if (XmlTransfer.isNew)
                {
                    if (byteStr != null)
                    {
                        if (XmlTransfer.xmlList.size() == XmlTransfer.size)
                        {
                            XmlTransfer.xmlList.remove(0);
                            XmlTransfer.xmlNameList.remove(0);
                        }
                        XmlTransfer.xmlList.add(byteStr);
                        XmlTransfer.xmlNameList.add(XmlTransfer.xmlName2);
                    }
                    byteStr = new byte[XmlTransfer.length];
                    in = XmlTransfer.length;
                }
                
                tmp = Receiver.available();
                Receiver.read(byteStr, XmlTransfer.length - in, Receiver
                        .available());
                in = in - tmp;
                XmlTransfer.critical.releaseReadLock();
            }
            if (XmlTransfer.xmlList.size() == XmlTransfer.size)
            {
                XmlTransfer.xmlList.remove(0);
                XmlTransfer.xmlNameList.remove(0);
            }
            XmlTransfer.xmlList.add(byteStr);
            XmlTransfer.xmlNameList.add(XmlTransfer.xmlName1);
            Receiver.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
